﻿/*
	Copyright (c) 第三界主 2014
	代码使用MIT协议发布
*/

#include "WTK.h"
#include <string.h>
#include <stdio.h>


class WaylandApplication
{
public:
	BOOL InitApplication()
	{

	}

};

static void paint_pixels(void *image, int padding, int width, 
	int height, uint32_t time)
{
	const int halfh = padding + (height - padding * 2) / 2;
	const int halfw = padding + (width - padding * 2) / 2;
	int ir, or;
	uint32_t *pixel = image;
	int y;

	/* squared radii thresholds */
	or = (halfw < halfh ? halfw : halfh) - 8;
	ir = or - 32;
	or *= or;
	ir *= ir;

	pixel += padding * width;
	for (y = padding; y < height - padding; y++) 
	{
		int x;
		int y2 = (y - halfh) * (y - halfh);

		pixel += padding;
		for (x = padding; x < width - padding; x++) 
		{
			uint32_t v;

			/* squared distance from center */
			int r2 = (x - halfw) * (x - halfw) + y2;

			if (r2 < ir)
				v = (r2 / 32 + time / 64) * 0x0080401;
			else if (r2 < or)
				v = (y + time / 32) * 0x0080401;
			else
				v = (x + time / 16) * 0x0080401;
			v &= 0x00ffffff;

			/* cross if compositor uses X from XRGB as alpha */
			if (abs(x - y) > 6 && abs(x + y - height) > 6)
				v |= 0xff000000;

			*pixel++ = v;
		}

		pixel += padding;
	}
}

// 
// void PaintProc(HSURFACE hSurface)
// {
// 	PAINTSTRUCTURE ps;
// 	memset(&ps, 0, sizeof(ps));
// 	wlBeginPaint(hSurface, &ps);
// 
// 	memset(ps.lpBuffer->lpPixelBuffer, 0xff, ps.lpBuffer->length);
// 
// 	SIZE sz = wlGetSurfaceSize(hSurface);
// 	//paint_pixels(ps.lpBuffer->lpPixelBuffer, 20, sz.width, sz.height, ps.uTime);
// 	
// 	HCAIROSURFACE hCairoSurface = ps.lpBuffer->hCairoSurface; // 类似与 HWND
// 	HCAIRO hdc = cairo_create(hCairoSurface); // 类似于 HDC
// 
//  	cairo_set_source_rgb(hdc,0, 0.4, 0.8);
//  	cairo_set_line_width(hdc, 50);
//  	cairo_move_to(hdc, 0, 0);
//  	cairo_line_to(hdc, sz.width, 0);
//  
//  	cairo_stroke(hdc);
//  
//  	cairo_set_source_rgb(hdc, 0 , 0, 0);
//  	cairo_move_to(hdc, 10, 20);
//  	cairo_set_font_size(hdc, 18);
//  	cairo_show_text(hdc, "这是标题栏");
//  	cairo_stroke(hdc);
// 
// 
// 	int btnCloseHeight = 20;
// 	int btnCloseWidth = 20;
// 	int btnCloseLineWidth = 2;
// 	cairo_set_source_rgb(hdc, 1, 0, 0);
// 	cairo_set_line_width(hdc, 2);
// 	cairo_move_to(hdc, sz.width-btnCloseWidth, btnCloseLineWidth/2); cairo_line_to(hdc, sz.width, btnCloseLineWidth/2);
// 	cairo_move_to(hdc, sz.width-btnCloseWidth, 0); cairo_line_to(hdc, sz.width-btnCloseWidth, btnCloseHeight);
// 	cairo_move_to(hdc, sz.width, 0); cairo_line_to(hdc, sz.width, btnCloseHeight);
// 	cairo_move_to(hdc, sz.width-btnCloseWidth, btnCloseHeight); cairo_line_to(hdc, sz.width, btnCloseHeight);
// 	cairo_move_to(hdc, sz.width-btnCloseWidth, btnCloseLineWidth/2); cairo_line_to(hdc, sz.width, btnCloseHeight);
// 	cairo_move_to(hdc, sz.width, btnCloseLineWidth/2); cairo_line_to(hdc, sz.width-btnCloseWidth, btnCloseHeight);
// 
// 	cairo_stroke(hdc);
// 
// 	cairo_set_source_rgb(hdc, 0 , 0, 0);
// 	cairo_move_to(hdc, 10, sz.height/2);
// 	cairo_set_font_size(hdc, 30);
// 	cairo_show_text(hdc, "你好, 中国!");
// 	cairo_stroke(hdc);
// 
// 	cairo_set_source_rgb(hdc, 0 , 0, 0);
// 	cairo_move_to(hdc, 10, sz.height/2+30);
// 	cairo_set_font_size(hdc, 18);
// 	cairo_show_text(hdc, "--第三界主");
// 	cairo_stroke(hdc);
// 
// 
// 	cairo_destroy(hdc);
// 
// 	wlRequestPaintDone(hSurface);
// 	wlEndPaint(hSurface, &ps);
// }

// void PaintDone(HSURFACE hSurface)
// {
// 	printf("PaintDone\n");
// 	//wlRequestPaintDone(hSurface);
// }
// 
// void PointerProc(HSURFACE hSurface, uint32_t button, uint32_t state, uint32_t serial)
// {
// 	LPSURFACEPRIVATE lpSurfacePrivate = wlGetSurfacePrivate(hSurface);
// 
// 	/* 鼠标左键按下了，移动窗口 */
// 	if (BTN_LEFT == button && state == WL_POINTER_BUTTON_STATE_PRESSED)
// 	{
// 		HSHELLSURFACE hShellSurface = lpSurfacePrivate->hShellSurface;
// 		//wl_shell_surface_move(hShellSurface, wlGetRegistry()->s_seat, serial);
// 		return;
// 	}
// }

int main(int argc, char **argv)
{
	WAYLANDGLOBALS object;
	GetWaylandGlobals(&object);

	int width = 400, height = 300;

	//HSURFACE surface = wlCreateSurface("wayland-hello", width, height);

// 	wlSetPaintProc(surface, PaintProc);
// 	wlSetPaintDone(surface, PaintDone);
// 	wlSetPointerProc(surface, PointerProc);

//	wlShowSurface(surface);

	int ret = 0;
	while (ret != -1)
	{
		;
	}

	return 0;
}

